<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.*"%>
<%@include file="../taglib.jsp" %>
<%@ page isELIgnored="false"%>
<%-- <%String language =(String)session.getAttribute("languageType"); %>
<fmt:setLocale value="<%=language %>"/>  --%>
<%-- <fmt:setLocale value="zh_CN"/>  --%>
<html>
<head>
<style type="text/css">
div.param {
	display: inline-block;
	*display: inline;
	*zoom: 1;
}

div.param2 {
	display: inline-block;
	*display: inline;
	*zoom: 1;
}

.btn-new-css {
    width: 80px;
	line-height: 32px;
	text-align: center;
	font-weight: bold;
	border-radius: 5px;
	margin: 0 20px 20px -20px;
	position: relative;
	overflow: hidden;
	color: #2E2E2E;
	text-shadow: 0px 1px 1px #fff;
	border: 1px solid #dce1e6;
	box-shadow: 0 1px 2px #fff inset,0 -1px 0 #A9D0F5 inset;
	background: -webkit-linear-gradient(top,#e4e8ec,#CEE3F6);
	background: -moz-linear-gradient(top,#f2f3f7,#e4e8ec);
    /*background: url('../resources/images/drillup.png') no-repeat 0 0 !important;*/
}

</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>${chartName}</title>
<link rel="stylesheet" href="../assets/ext/resources/css/ext-all.css" />

<script type="text/javascript" src="../js/comm/jquery.js"></script>
<script type="text/javascript" src="../assets/anychart/js/AnyChart.js"></script>
<script type="text/javascript"
	src="../assets/anychart/js/AnyChartHTML5.js"></script>
<script type="text/javascript" src="../assets/ext/ext-base.js"></script>
<script type="text/javascript" src="../assets/ext/ext-all.js"></script>
<%
Locale loc=new Locale("en","US");
response.setLocale(loc);
	String browserLang = request.getLocale().toString();
	System.out.println(request.getLocale().toString()+"  show");
	String extLocalUrl="";
	//String urlParam = request.getQueryString();
	String legendLang = "";
	
	String language =(String)session.getAttribute("languageType");
	if (language!= null) {
		browserLang = language;
		if(!"en_US".equals(browserLang)){
			extLocalUrl="../assets/ext/ext-lang-"+browserLang+".js";	
		}
		legendLang = browserLang.replace("_", "-");
	} else {
		extLocalUrl="../assets/ext/ext-lang-"+browserLang+".js";
		legendLang = browserLang.replace("_", "-");
	}
	
	/* String language1 =(String)session.getAttribute("languageType");
	if(language1!=null){
		browserLang=language1;
	}
	if (urlParam.contains("&")) {
		browserLang = urlParam.split("&")[1];
		if(!"en_US".equals(browserLang)){
			extLocalUrl="../assets/ext/ext-lang-"+browserLang+".js";	
		}
		legendLang = browserLang.replace("_", "-");
	} else {
		extLocalUrl="../assets/ext/ext-lang-"+browserLang+".js";
		legendLang = browserLang.replace("_", "-");
	} */
%>
<fmt:setLocale value="<%=browserLang %>"/>
<fmt:setBundle basename="messages"/>
<script type="text/javascript" src="<%=extLocalUrl %>"></script>
<script type="text/javascript" src="../assets/ux/gnamespace.js"></script>

<script type="text/javascript">
	function showDate(paramCd, k) {
		//日期型参数，添加ext日期组件  author: ghq
		var md = new Ext.form.DateField({
			id : paramCd,
			// 		           name:paramCd,  
			editable : true, //不允许对日期进行编辑  
			width : 100,
			format : "Ymd",
			emptyText : "<fmt:message key="chart.DateField_select" />"
		});
		md.render((paramCd + k));
	}

	function showSelect(paramCd, name, tableName, colType, k) {
		//除日期型参数之外，添加下拉框组件      author:  ghq
		var store = new Ext.data.Store({// 配置分组数据集
			baseParams : {
				table : tableName,
				column : paramCd,
				columnId : name,
				colType : colType
			},
			reader : new Ext.data.JsonReader({
				totalProperty : "count",
				root : "conditonVal",
				// 要读取出来的字段
				fields : [ 'value' ]
			}),
			proxy : new Ext.data.HttpProxy({
				url : [ biAdhocNs.sysUrl, 'chart/chart/defaultValue' ].join('')
			})
		});

		store.load({
			params : {
				start : 0,
				limit : 10
			}
		});//传递参数

		var combobox = new Ext.form.ComboBox({
			//日期型参数，添加ext日期组件
			id : paramCd,
			width : 120,
			renderTo : paramCd + k,
			triggerAction : "all",//是否开启自动查询功能
			store : store,//定义数据源
			displayField : "value",//关联某一个逻辑列名作为显示值
			// 				 valueField: "cid",//关联某一个逻辑列名作为实际值
			mode : "local",//如果数据来自本地用local 如果来自远程用remote默认为remote
			emptyText : "<fmt:message key="chart.select" />" //,//没有选择时候的默认值
		});

		combobox.show();
	}
</script>
</head>
<body style="align: center; overflow-y: hidden;">
	<div id="paramDiv"
		style="text-align: center; width: 100%; margin: 0 auto;"></div>
	<div id="chartContainer"
		style="align: center; border: 2px; width: auto; overflow-y: hidden;"></div>

	<script type="text/javascript">
		//下钻时level加1，上钻时level减1。
		//对于array下钻时加一个元素，上钻时减两个元素。
		//所有操作如果失败则撤销
		var drillLevel = 0;
		var drillValueArray = new Array();
		var chartId = '${id}';
		var dirllType = "down";
		//图表上钻
		function drillUpChart() {
			dirllType = "up";
			if (drillLevel < 1)
				return false;
			drillLevel--;
			drillValueArray.pop();
			if (drillLevel < 1) {//上钻到第一级
				show("");
				document.getElementById("toHighLever2").style.display = 'none';
			} else {
				drillChart(drillValueArray[drillLevel - 1]);
			}
		}
		//图表下钻
		function drillFunName(id, val) {
			dirllType = "down";
			drillLevel++;
			drillValueArray.push(id);
			drillChart(id);

		}
		function drillChart(id) {
			$
					.ajax({
						type : 'POST',
						url : '/biapp-01-cx/chart/chartDrillCheck',
						data : {
							chartId : chartId,
							level : drillLevel,
							dirllType : dirllType
						},
						success : function(data) {
							if (data.length > 2) {
								var jsonDrill = eval("(" + data + ")");
								if (jsonDrill.drillStatus == "true") {
									drillDown(jsonDrill, id);
									if (drillLevel > 0) {
										document.getElementById("toHighLever2").style.display = 'block';
									}
								} else {
									if (dirllType == "down") {
										drillLevel--;
										drillValueArray.pop();
									} else {
										drillLevel++;
										drillValueArray.push(id);
									}
									return false;
								}

							}
						},
						error : function() {
						}
					});
		}

		function drillDown(drillJsonObj, id) {
			var idValue = id;
			var columnId = drillJsonObj.columnId;
			var namePath = drillJsonObj.namePath;
			var conditionId = drillJsonObj.conditionId;
			var tableName = drillJsonObj.tableName;
			var params = '';
			var url = '/biapp-01-cx/chart/chartDrillDown?chartId=' + chartId
					+ "&columnId=" + columnId + "&namePath=" + namePath
					+ "&conditionId=" + conditionId + "&idValue=" + idValue
					+ "&tableName=" + tableName
					+ "&params=" + params;
			Ext.Ajax.request({
				url : url,
				timeout : 3600000,
				method : 'POST',
				headers: {
					'Accept-Language':'<%=legendLang%>'
				},
				params : {
					mulRelationData : '${mulRelationData}' // 多表关联参数
				},
				callback : function(options, success, response) {
					if (success == true) {
						var chart = new AnyChart('../assets/anychart/swf/AnyChart.swf',
						'../assets/anychart/swf/Preloader.swf');
						chart.wMode = 'transparent';
						chart.width = '100%';
						chart.height = '93%';
						chart.setXMLData(response.responseText);
						chart.write("chartContainer");
					}
				}
			});
			//showParam(id);
		}

		function show(params) {
			/*显示图表   author: ghq*/
			var id = '${id}';
			var url = '/biapp-01-cx/chart/viewEditChart?chartId=' + id
					+ "&params=" + params;
			Ext.Ajax.request({
				url : url,
				timeout : 3600000,
				method : 'POST',
				headers: {
					 'Accept-Language':'<%=legendLang%>'
				   },
				params : {
				},
				callback : function(options, success, response) {
					if (success == true) {
						var chart = new AnyChart('../assets/anychart/swf/AnyChart.swf',
						'../assets/anychart/swf/Preloader.swf');
						chart.wMode = 'transparent';
						chart.width = '100%';
						chart.height = '93%';
						chart.setXMLData(response.responseText);
						chart.write("chartContainer");
						showParam(id);
					}
				}
			});
			
		}

		function showParam(chartId) {
			// 拼接显示图表所携带形参参数   author: ghq
			$("#paramDiv").empty();
			$
					.ajax({
						type : 'POST',
						url : '/biapp-01-cx/chart/chart/getChartParams',
						data : {
							chartId : chartId
						},
						success : function(data) {
							if (data.length > 2) {
								var jsonObj = eval("(" + data + ")");
								if (jsonObj.length > 0) {
									$("#paramDiv")
											.append("</br><fmt:message key="chart.parameter_settings" />: &nbsp; ");
									var k = 1;
									$
											.each(
													jsonObj,
													function(i, item) {
														var paramCd = item.paramCd;
														var name = item.paramName;
														var value = item.defaultValue;
														var colType = item.colType;
														var tableName = item.tableName;
														var paramName2 = item.paramName2;
														$("#paramDiv").append(name + ":");
														if (colType == '3' || colType == '4') {
															$("#paramDiv").append(
																			"&nbsp;<div id='" + paramCd + k +"' name='" + paramCd + "' class='param' ></div>&nbsp;&nbsp;");
															showDate(paramCd, k);
														} else {
															$("#paramDiv").append("&nbsp;<div id='" + paramCd + k+"'  name='" + paramCd + "' class='param' ></div>&nbsp;&nbsp;");
															showSelect(paramCd,
																	paramName2,
																	tableName,
																	colType, k);
														}

														k++;
													});
									$("#paramDiv")
											.append(
													"&nbsp;&nbsp;<div id='subm1' class='param2'><input type='button' style='height:25px;width:45px;' value='<fmt:message key="button.ok" />' onclick='javascript:updateChart(\""
															+ chartId
															+ "\");' /></div>");
									$("#paramDiv")
											.append(
													"&nbsp;&nbsp;&nbsp;<div id='toHighLever2' class='param2' style='display:none;'> <input type= 'button' value='<fmt:message key="button.return" />' class='btn-new-css' onclick='javascript:drillUpChart();'/></div> ");
									var ch = $("<input type= 'button' value='<fmt:message key="button.i18nZh" />' />").click(function(){
										var url = location.href;
										var url2 = [ biAdhocNs.sysUrl, 'chart/i18n' ].join('');
										Ext.Ajax.request({
											url:url2,
											method:"post",
											params:{
												language:"zh_CN"
											},
											callback : function(options, success, response) {
												if (success == true) {
													window.location = url;
												}
											}
										});
									});
									var en = $("<input type= 'button' value='<fmt:message key="button.i18nEn" />' />").click(function(){
										var url = location.href;
										var url2 = [ biAdhocNs.sysUrl, 'chart/i18n' ].join('');
										Ext.Ajax.request({
											url:url2,
											method:"post",
											params:{
												language:"en_US"
											},
											callback : function(options, success, response) {
												if (success == true) {
													window.location = url;
												}
											}
										});
									});
									var language = $('<div style="float:right; margin-right:100px;">').append(ch).append(en).append('</div>');
									$("#paramDiv").append(language);
								}
							} else {
								$("#paramDiv").append("</br></br></br> ");
								$("#paramDiv")
										.append(
												"<div id='toHighLever2' class='param2' style='display:none;'> <input type= 'button' value='<fmt:message key="button.return" />' class='btn-new-css' onclick='javascript:drillUpChart();'/></div> ");
							}
						},
						error : function() {
							Ext.Msg.alert("提示", "数据源加载失败，请稍后再重试！");
						}
					});
		}

		function updateShow(params) {
			// 根据传递参数，更改图表显示    author: ghq
			var id = '${id}';
			var url = '/biapp-01-cx/chart/viewEditChart?chartId=' + id
					+ "&params=" + params;
			Ext.Ajax.request({
				url : url,
				timeout : 3600000,
				method : 'POST',
				headers: {
					'Accept-Language':'<%=legendLang%>'
				},
				params : {
				},
				callback : function(options, success, response) {
					if (success == true) {
						var chart = new AnyChart('../assets/anychart/swf/AnyChart.swf',
						'../assets/anychart/swf/Preloader.swf');
						chart.wMode = 'transparent';
						chart.width = '100%';
						chart.height = '93%';
						chart.setXMLData(response.responseText);
						chart.write("chartContainer");
					}
				}
			});
		}

		function updateChart(id) {
			// 获取并拼装参数栏参数     author: ghq
			var tempParam = "{";
			var temp = "";
			$(".param").each(function(i) {
				var name = $(this).attr("name");
				var value = Ext.get(name).getValue();
				temp = name + "=" + value + ",";
				if (value.indexOf("<fmt:message key="chart.select" />") >= 0) {
					temp = "";
				}
				tempParam += temp;
			});
			tempParam = tempParam + "}";
			updateShow(tempParam);
		}

		window.onload = show('');
	</script>
</body>
</html>